/**
 * axios 封装
 */
import axios from 'axios'
import router from '@/router'
import { useLoginStore } from '@/stores/login'

const http = axios.create({
  // baseURL: "http://stellafootwear.com.cn:777",
  // baseURL: "http://192.168.130.241:777",
  // baseURL: "http://192.168.2.7:888",
  // baseURL: "http://192.168.100.100:8080",
  // baseURL: "http://10.1.11.128:8080",
  baseURL: "http://localhost:8080",
  timeout: 60000
})

const loginStore = useLoginStore()

// 请求拦截
http.interceptors.request.use(
  config => {
    const token = sessionStorage.getItem("token")
    config.headers.Authorization = token
    return config
  },
  err => Promise.reject(err)
)

// 响应拦截
http.interceptors.response.use(
  res => {
    if (res.data.reset) {
      sessionStorage.removeItem("token")
      router.replace({ name: 'login'})
      loginStore.isLogin = false
    } else {
      return res
    }
  },
  err => Promise.reject(err)
)

export default http